package com.example.teamtribe.main.club.clubintroduce.media.photo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.Media;
import com.example.teamtribe.main.club.clubintroduce.GridSpacingItemDecoration;
import com.example.teamtribe.utils.Utility;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

public class AddPhotoActivity extends BaseActivity implements AddPhotoContract.View{
    private PhotoGridAdapter adapter;
    private RecyclerView recyclerView;
    private List<LocalMedia> selectList;
    private AddPhotoContract.Presenter presenter;
    private int albumId;
    private int teamId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_add);
        ((TextView)findViewById(R.id.tv_title)).setText("上传照片");
        albumId=((Media)getIntent().getParcelableExtra("media")).getRealId();
        teamId=((Media)getIntent().getParcelableExtra("media")).getTeamId();
        recyclerView = findViewById(R.id.rv_photo_list);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(4, 5, false));
        recyclerView.setNestedScrollingEnabled(false);
        adapter=new PhotoGridAdapter(PhotoGridAdapter.LOCALMEDIA);
        recyclerView.setAdapter(adapter);
        presenter = new AddPhotoPresenter(this);

        PictureSelector.create(AddPhotoActivity.this)
                .openGallery(PictureMimeType.ofImage())
                .forResult(PictureConfig.CHOOSE_REQUEST);//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()

        ((Button)findViewById(R.id.bt_upload)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.addPhotos(Utility.getToken(),selectList,teamId,albumId);
            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    adapter.setList(selectList);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    }

    @Override
    public void next() {
        finish();
    }
}
